package org.magicbox.qjunTPO.controller;


import org.magicbox.qjunTPO.common.lang.Result;
import org.magicbox.qjunTPO.entity.Supuser;
import org.magicbox.qjunTPO.entity.SysMessage;
import org.magicbox.qjunTPO.entity.User;
import org.magicbox.qjunTPO.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 欢迎添加科技实践队粉丝群：690849711
 * @since 2021-11-18
 */
@RestController
@RequestMapping("/sys-message")
public class SysMessageController {
    @Autowired
    SysMessageService sysMessageService;

    @Autowired
    PoemsService poemsService;

    @Autowired
    ApplyAllService applyAllService;

    @Autowired
    CommentsService commentsService;

    @Autowired
    UserService userService;

    /**
     * 增加通知
     * @return
     */

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @PreAuthorize("hasAnyAuthority('EventPower')")
    public Result add(@Validated @RequestBody SysMessage sysMessage){   //resid不能用res_id
//        type  默认0.全体通知	1.下面的resid是poemid, 2. 下面的id是申请id ,3.下面的id是commentid 4.下面的id是用户id
//            检测目标内容是否存在
            if (!sysMessageService.exResId(sysMessage,poemsService,applyAllService,commentsService,userService)) {
                return Result.fail("内容不存在");
            }
            boolean saveOk = sysMessageService.addSysMsg(sysMessage);
        if (saveOk) {
            return Result.success("成功");
        }else {
            return Result.fail("失败");
        }
//
//        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//            Integer suid =new Integer(authentication.getName());
//
//            sysMessage.setSuid(suid);
//            sysMessage.setMsgCreate(LocalDateTime.now());
//            try {
//                sysMessageService.save(sysMessage);
//                return Result.success("成功");
//            }catch (Exception e){
//                return Result.fail("失败");
//            }
    }
//
//    public Result add(@Validated @RequestBody Supuser supuser,
//                      @PathParam("content")String content,
//                      @PathParam("uid")Integer uid,
//                      @PathParam("type")Integer type,
//                      @PathParam("resid")Long resid){   //resid不能用res_id
////        type  默认0.全体通知	1.下面的resid是poemid,2. 下面的id是申请id,3.下面的id是commentid 4.下面的id是用户id
//        if (supuserService.exPower(supuser.getSuId(),3)&supuserService.passowrdProving(supuser)) {
//            SysMessage sysMessage = new SysMessage(uid,content,type,resid);
////            检测目标内容是否存在
//            if (!sysMessageService.exResId(sysMessage,poemsService,applyAllService,commentsService,userService)) {
//                return Result.fail("内容不存在");
//            }
//            sysMessage.setSuid(supuser.getSuId());
//            sysMessage.setMsgCreate(LocalDateTime.now());
//            try {
//                sysMessageService.save(sysMessage);
//                return Result.success("成功");
//            }catch (Exception e){
//                return Result.fail("失败");
//            }
//        }else return Result.fail("无权限");
//    }


}
